//
//  GalleryView.m
//  ios_news
//
//  Created by Han on 14-7-6.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "MoreGalleryView.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"
#import "Gallery.h"

@implementation MoreGalleryView

- (id) initWithFrame:(CGRect)frame :(NSArray *)data {
    self = [super initWithFrame:frame];
    
    mFrame = frame;
    
    mNum = (int)(data.count + 2);
    
    mData = [NSMutableArray arrayWithCapacity:mNum];
    
    for (int i = 0; i < mNum; i++) {
        if (i == 0) {
            [mData addObject:[data lastObject]];
        } else if (i == mNum - 1) {
            [mData addObject:[data firstObject]];
        } else {
            [mData addObject:[data objectAtIndex:i-1]];
        }
    }

    
    if (self) {
        
        [self drawView];
    }
    
    return self;

}


- (void) drawView
{
    mGalleryScrollView = [[UIScrollView alloc] initWithFrame:mFrame];
    mGalleryScrollView.showsHorizontalScrollIndicator = NO;
    mGalleryScrollView.pagingEnabled = YES;
    mGalleryScrollView.delegate = self;
    
    //NSLog(@"%@", mData);
    //NSArray *imgArray = @[@"2.png", @"0.png", @"1.png", @"2.png", @"0.png"];
    
    
    for (int i = 0; i < mNum; i++) {
        UIImageView *img = [[UIImageView alloc] init];
        img.tag = i;
        
        Gallery *gallery = [mData objectAtIndex:i];
        
        [img sd_setImageWithURL:[NSURL URLWithString:gallery.imgUrl]
                    placeholderImage:[UIImage imageNamed:@"more_gallery_holder"]];
        
        img.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickGallery:)];
        [img addGestureRecognizer:singleTap];
        
        
        [img setFrame:CGRectMake(320 * i, 0, 320, 120)];
        [mGalleryScrollView addSubview:img];
    }
    
    // 必须给出内容的实际大小，滚动视图才知道滚动的范围
    mGalleryScrollView.contentSize = CGSizeMake(320 * mNum, 120);
    
    mCurrentPage = 1;
    
    // 设置显示区域
    CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
    [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    
    
    [self addSubview:mGalleryScrollView];
    
    
    mPageControl = [[UIPageControl alloc] init];
    mPageControl.numberOfPages = mNum - 2;
    mPageControl.currentPage = 0;
    mPageControl.enabled = YES;
    mPageControl.currentPageIndicatorTintColor = MAIN_COLOR;
    mPageControl.pageIndicatorTintColor = [UIColor whiteColor];
    mPageControl.center = CGPointMake(160, self.frame.size.height*0.9);
    
    [self addSubview:mPageControl];
    
    
    // 循环播放
    [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
}

// 自动翻页
- (void) scrollToNextPage:(id)sender {
    
    CGRect visableImage = CGRectMake((mCurrentPage+1) * mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
    [mGalleryScrollView scrollRectToVisible:visableImage animated:YES];
    
    //NSLog(@"%ld", (long)[mPageControl currentPage]);
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    NSInteger tempCurrentPage = (NSInteger) (scrollView.contentOffset.x/320);
    if (mCurrentPage == tempCurrentPage) {
        return;
    }
    
    mCurrentPage = tempCurrentPage;
    if (mCurrentPage == mNum - 1) {
        // 最后一页 设置当前页为0
        [mPageControl setCurrentPage:0];
        
        // 向后翻页，隐蔽的覆盖第一页
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    } else if (mCurrentPage == 0) {
        [mPageControl setCurrentPage:mNum - 2];
        
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width * (mNum - 1), 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    } else {
        [mPageControl setCurrentPage: mCurrentPage - 1];
    }
}



-(void)onClickGallery: (UITapGestureRecognizer *)recognizer {
    
    int index = (int)recognizer.view.tag;
    Gallery *gallery = [mData objectAtIndex:index];
    
    //NSLog(@"图片被点击!%@", gallery.imgUrl);
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:gallery.url]];
}

/*
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    //NSLog(@"+++++++");
    //CGPoint contentOffset = scrollView.contentOffset;
    mCurrentPage = (NSInteger) (scrollView.contentOffset.x/320);
    
    if (mCurrentPage == 4) {
        // 最后一页 设置当前页为0
        [mPageControl setCurrentPage:0];
        
        // 向后翻页，隐蔽的覆盖第一页
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    } else if (mCurrentPage == 0) {
        [mPageControl setCurrentPage:2];
        
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width * 3, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    } else {
        [mPageControl setCurrentPage: mCurrentPage - 1];
    }
}
 */
 
@end
